package com.flyweight.main;

import com.flyweight.FlyweightFactory;
import com.flyweight.face.Flyweight;
import com.flyweight.impl.ConcreateFlyweight;

/**
 * Flyweight模式：缓存(共享)可以共同的对象、属性，避免创建大量的重复对象、内容。
 * 
 * 为重复或可共享的对象、属性设置一个缓冲，称为内部属性。这些内部属性一般情况下都是不可修改的
 * ，也就是在第一个对象、属性被创建后，就不会去修改了(否则就没意义了)。
 * 
 * 除了共享对象、属性之外，还有一些是基于应用环境，或是实时数据，这些无法共享的东西我们称为外部状态。
 * 
 * 在Flyweight模式应用中，通常修改的是外部状态属性，而内部状态属性一般都是用于参考或计算时引用。
 * 
 * @author dell
 * 
 */
@SuppressWarnings("unused")
public class Client {

	FlyweightFactory factory = FlyweightFactory.getInstance();

	public Client() {
		for (int i = 0; i < 10000; i++) {
			Flyweight f1 = factory.getFlyweight("baidu");
			// f1.operation();
			Flyweight f2 = factory.getFlyweight("google");
			// f2.operation();
			Flyweight f3 = factory.getFlyweight("baidu");
			// f3.operation();
			Flyweight f4 = factory.getFlyweight("google");
			// f4.operation();
		}
		System.out.println(factory.getFlyweightsSize());
	}

	public Client(String s) {
		for (int i = 0; i < 10000; i++) {
			Flyweight f1 = new ConcreateFlyweight("baidu");
			// f1.operation();
			Flyweight f2 = new ConcreateFlyweight("google");
			// f2.operation();
			Flyweight f3 = new ConcreateFlyweight("baidu");
			// f3.operation();
			Flyweight f4 = new ConcreateFlyweight("google");
			// f4.operation();
		}
	}

	public static void main(String[] args) {
		// Client client = new Client();
		Client client = new Client("");
		Runtime r = Runtime.getRuntime();
		System.out.println(r.freeMemory());
	}

}
